//判断是否的登录
function goodsDetail_login() {
    var user = document.getElementById("top1_login").innerText;
    var top1 = document.getElementById("top1");
    var container_top = document.getElementById("container_top");
    if (user.trim().length == 0){
        document.getElementById("top1_login").innerText = "登录";
        document.getElementById("top2_gwc").innerText = "0";
        top1.style.width = "140px";
        container_top.style.width = "240px";
        container_top.style.marginLeft = "288px";
    }else if (user.trim().length != 0 && user != "登录"){
        document.getElementById("top1_login").setAttribute("href","#");
        top1.style.width = "195px";
        container_top.style.width = "300px";
        container_top.style.marginLeft = "234px";
    }
}

function Order_to_load() {
    var top1_login = document.getElementById("top1_login").innerText;
    if (top1_login == "登录"){
        window.location.href = "/MyXiaomiShop/login.jsp";
    }else {
        $(function () {
            $.ajax({
                url: "orderListServlet?user_id="+document.getElementById("top1_login").innerText,
                type: "get",
                dataType: "json",
                success: function (res) {
                    var shopping_tbody = $("#shopping_tbody_orders");
                    shopping_tbody.empty();
                    for (var i = 0; i < res.length; i++) {
                        var tr = document.createElement("tr");
                        tr.style.fontSize = "13px";

                        var td_1 = document.createElement("td");
                        td_1.innerText = i+1;

                        var td_2 = document.createElement("td");
                        td_2.innerText = res[i].ordernumber;

                        var td_3 = document.createElement("td");
                        td_3.innerText = res[i].aggregate;

                        var td_4 = document.createElement("td");
                        td_4.style.color = "red"
                        if (res[i].state == "已送达未签收"){
                            var select = document.createElement("select");
                            select.setAttribute("onchange","state(id)")
                            select.setAttribute("id",res[i].ordernumber);
                            var option_1 = document.createElement("option");
                            option_1.innerText = res[i].state;
                            option_1.setAttribute("selected","selected");
                            var option_2 = document.createElement("option");
                            option_2.innerText = "已签收";
                            select.appendChild(option_1);
                            select.appendChild(option_2);
                            td_4.appendChild(select);
                        }else {
                            td_4.innerText = res[i].state;
                        }

                        var td_5 = document.createElement("td");
                        td_5.innerText = res[i].orderSubmissionTime;

                        var td_6 = document.createElement("td");
                        td_6.innerText = res[i].receiverAddress;

                        var td_7 = document.createElement("td");
                        var input = document.createElement("input");
                        input.setAttribute("id",res[i].ordernumber);
                        input.setAttribute("value","订单详情");
                        input.setAttribute("onclick","order_details(id)")
                        input.setAttribute("type","button");
                        td_7.appendChild(input);

                        tr.appendChild(td_1);
                        tr.appendChild(td_2);
                        tr.appendChild(td_3);
                        tr.appendChild(td_4);
                        tr.appendChild(td_5);
                        tr.appendChild(td_6);
                        tr.appendChild(td_7);
                        shopping_tbody.append(tr);
                    }
                },
                error: function (res) {
                    alert("所有购物车获取失败！")
                }
            });
        });
    }
}
//商品状态下拉框事件
function state(e) {
    var msg = "你确定当前订单: "+e+" 已签收了吗？？";
    if(confirm(msg)==true) {
        var merchant_state = document.getElementById(e).value;
        $.ajax({
            url: "merchant_StateServlet?order_number=" + e + "&merchant_state=" +merchant_state,
            type: "get",
            dataType: "json",
            success: function (res) {
                if (res){
                    alert("订单签收已提交！！");
                    location.reload();
                }
            },
            error: function (res) {
                alert("获取失败，请联系程序员解决!");
            }
        });
    }else {
        return false;
    }
}

function order_details(e) {
    window.location.href = "/MyXiaomiShop/orderList_orderDatail?orderList_ordernumber=" + e;
}